﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="GenerateClusterConfigFileOperation.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace CloudBlitz.DeploymentManager.DeploymentOperations
{
    using System;

    using CloudBlitz.DeploymentManager.DeploymentWorkflow;

    using Microsoft.Hpc.Azure.ClusterConfig;

    public class GenerateClusterConfigFileOperation : DeploymentOperationBase
    {
        public GenerateClusterConfigFileOperation() : base(2)
        {
        }

        protected override OperationResult ExecuteOverride(DeploymentContext context)
        {
            try
            {
                this.RaiseProgressChangedEvent(0, "Generating cluster config file");

                var config = new ClusterConfig();
                config.SetCsdefFile(context.ClusterDetails.ServiceDefinitionFilePath);

                config.EnableSOA();

                config.SubScriptionId = new Guid(context.AzureCredentials.SubscriptionId);

                config.ServiceName = context.HostedServiceName;
                config.SetStorage(context.StorageAccountName, context.ClusterDetails.StorageKey);

                // fill in the SQL Azure account information
                config.DBServer = context.SqlServerDetails.Name;
                config.DBUser = context.SqlServerDetails.Credentials.AdministratorLogin;
                config.DBPassword = context.SqlServerDetails.Credentials.AdministratorPassword;
                config.DBName = context.SqlServerDetails.DatabaseName;

                // fill in the certificate thumbprints
                config.AddCertificate("Microsoft.Hpc.Azure.Certificate.SSLCert", context.ClusterDetails.SSLCertificate);
                config.AddCertificate("Microsoft.Hpc.Azure.Certificate.PasswordEncryptionCert", context.ClusterDetails.PasswordEncryptionCertificate);

                // setup the built-in Deployment admin account
                config.ClusterAdmin = context.ClusterDetails.ClusterAdmin;
                config.ClusterAdminEncryptedPassword = CertificateHelper.EncryptWithCertificate(context.ClusterDetails.ClusterAdminPassword, context.ClusterDetails.PasswordEncryptionCertificate);
                config.ClusterName = context.ClusterDetails.Name;

                // setup the numbers of instances for different kinds of nodes
                config.SetInstanceCount(context.ClusterDetails.HeadNodeName, context.ClusterDetails.HeadNodeInstanceCount);
                config.SetInstanceCount(context.ClusterDetails.ComputeNodeName, context.ClusterDetails.ComputeNodeInstanceCount);

                config.Generate(
                            context.ClusterDetails.ServiceConfigurationFilePath,
                            context.ClusterDetails.ServiceConfigurationFilePath);

                this.RaiseProgressChangedEvent(100, "Cluster config file generated");

                return OperationResult.Success;
            }
            catch (Exception e)
            {
                return ExceptionHelper.HandleException(e);
            }
        }
    }
}